"""
用户相关的Pydantic模型
"""
from typing import Optional, List
from datetime import datetime
from pydantic import BaseModel, EmailStr, Field


class UserBase(BaseModel):
    """用户基础模型"""
    username: str = Field(..., min_length=3, max_length=50, description="用户名")
    email: Optional[EmailStr] = Field(None, description="邮箱")
    full_name: Optional[str] = Field(None, max_length=100, description="姓名")
    role: str = Field("operator", description="角色")
    is_active: bool = Field(True, description="是否激活")
    phone: Optional[str] = Field(None, max_length=20, description="电话号码")
    department: Optional[str] = Field(None, max_length=100, description="部门")
    description: Optional[str] = Field(None, description="用户描述")


class UserCreate(UserBase):
    """创建用户模型"""
    password: str = Field(..., min_length=6, description="密码")


class UserUpdate(BaseModel):
    """更新用户模型"""
    email: Optional[EmailStr] = None
    full_name: Optional[str] = Field(None, max_length=100)
    role: Optional[str] = None
    is_active: Optional[bool] = None
    phone: Optional[str] = Field(None, max_length=20)
    department: Optional[str] = Field(None, max_length=100)
    description: Optional[str] = None


class UserPasswordUpdate(BaseModel):
    """更新密码模型"""
    current_password: str = Field(..., description="当前密码")
    new_password: str = Field(..., min_length=6, description="新密码")


class User(UserBase):
    """用户响应模型"""
    id: int
    avatar: Optional[str] = None
    created_at: datetime
    updated_at: datetime
    last_login: Optional[datetime] = None

    model_config = {"from_attributes": True}


class UserList(BaseModel):
    """用户列表响应模型"""
    users: List[User]
    total: int
    page: int
    page_size: int


class UserStats(BaseModel):
    """用户统计模型"""
    total_users: int
    active_users: int
    inactive_users: int
    roles_distribution: dict
    recent_logins: int


class LoginRequest(BaseModel):
    """登录请求模型"""
    username: str = Field(..., description="用户名")
    password: str = Field(..., description="密码")


class LoginResponse(BaseModel):
    """登录响应模型"""
    access_token: str
    token_type: str = "bearer"
    user: User